home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0167_Vector Line Intersection.pas < prev    next >
Pascal/Delphi Source File  |  1995-02-28  |  2KB  |  36 lines

  1. {
  2. FROM:    Paul R. Santa-Maria 71674,422, 71674,422
  3. TO:    Gayle Davis, 72067,2726
  4. DATE:    1/16/95 10:47 PM
  5. Re:    SWAG submission (GRAPHICS)
  6.  
  7. {The algorithm of this line intersection routine is based on vector
  8. cross products of the line endpoints.  The recursion is only one level 
  9. deep, to handle a specific degenerate case (collinear lines).  If the 
  10. degenerate case is detected, it nudges one end of one line and looks 
  11. to see if the intersection criterion is still fulfilled.  If not, it 
  12. nudges the other end and tries again.--Steve Schafer [CIS 76711,522]}
  13.  
  14.   function INTERSECT(L1X1, L1Y1, L1X2, L1Y2,
  15.                      L2X1, L2Y1, L2X2, L2Y2 : LongInt) : Boolean;
  16.   var
  17.     Z1, Z2, Z3, Z4 : LongInt;
  18.   begin
  19.     Z1 := L1X1*(L2Y2-L2Y1)+L2X2*(L2Y1-L1Y1)+L2X1*(L1Y1-L2Y2);
  20.     Z2 := L2X1*(L1Y1-L1Y2)+L1X1*(L1Y2-L2Y1)+L1X2*(L2Y1-L1Y1);
  21.     Z3 := L1X2*(L2Y1-L2Y2)+L2X1*(L2Y2-L1Y2)+L2X2*(L1Y2-L2Y1);
  22.     Z4 := L2X2*(L1Y2-L1Y1)+L1X2*(L1Y1-L2Y2)+L1X1*(L2Y2-L1Y2);
  23.     if (Z1 = 0) and (Z2 = 0) and (Z3 = 0) and (Z4 = 0) then
  24.       INTERSECT := (INTERSECT(L1X1, L1Y1, L1X2, L1Y2, L2X1, L2Y1, 
  25.                               L2X1+L2Y2-L2Y1, L2Y1+L2X1-L2X2) or 
  26.                     INTERSECT(L1X1, L1Y1, L1X2, L1Y2, L2X2+L2Y2-L2Y1,
  27.                               L2Y2+L2X1-L2X2, L2X2, L2Y2))
  28.     else if (((Z1 >= 0) and (Z2 >= 0) and (Z3 >= 0) and (Z4 >= 0)) or
  29.              ((Z1 <= 0) and (Z2 <= 0) and (Z3 <= 0) and (Z4 <= 0))) then
  30.       INTERSECT := True
  31.     else
  32.       INTERSECT := False;
  33.   end;
  34.  
  35.  
  36.